﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.CommandLineUtilities.Properties;

namespace Workflows.Components.CommandLineUtilities
{
    [Serializable]
    public class CommandLineComponent:WorkflowComponentBase
    {
        #region props
        private CommandLineInstruction _Instruction;
        private CommandLineOutput _Output;

        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction = (CommandLineInstruction) value; }
        }

        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output = (CommandLineOutput) value; }
        }

        /// <summary></summary>
        public string ProgramFilePath
        {
            get
            {
                return this._Instruction.ProgramFilePath;
            }
            set
            {
                this._Instruction.ProgramFilePath = value;
            }
        }
        /// <summary></summary>
        public string ArgTemplate
        {
            get
            {
                return this._Instruction.ArgTemplate;
            }
            set
            {
                this._Instruction.ArgTemplate = value;
            }
        }
        /// <summary></summary>
        public Dictionary<string, string> Options
        {
            get
            {
                return this._Instruction.Options;
            }
            set
            {
                this._Instruction.Options = value;
            }
        }
        #endregion

        public CommandLineComponent()
            : base()
        {
            this.ComponentName = "Exe Wrapper";
            this.Description = "Call command line applications";
            this.ComponentIcon = Resources.cmd_IDI_APPICON;
            this.ComponentImage = Resources.batch;
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.External_Processes.ToString();

            this._Instruction =
                new CommandLineInstruction(this.ComponentID, Guid.Empty, string.Empty, string.Empty, null);
            this._Output = new CommandLineOutput(this.ComponentID, Guid.Empty, null);
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ProgramFilePath", this.ProgramFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ArgumentTemplate", this.ArgTemplate);
            if (this.Options != null && this.Options.Count > 0)
            {
                XmlNode argNameRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "Options");
                foreach (string opName in this.Options.Keys)
                {
                    XmlNode opNode = XmlDataUtil.AddElement(ref xDoc, argNameRootNode, "Option");
                    XmlDataUtil.UpdateAttribute(ref xDoc, opNode, "Name", opName);
                    XmlDataUtil.UpdateAttribute(ref xDoc, opNode, "Value", this.Options[opName]);
                }
            }

            return xNode;
        }

        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                CommandLineComponent component = base.Instantiate(xNode) as CommandLineComponent;

                component.ProgramFilePath = xNode.Attributes.GetNamedItem("ProgramFilePath").Value;
                component.ArgTemplate = xNode.Attributes.GetNamedItem("ArgumentTemplate").Value;
                component.Options = null;
                XmlNodeList opNodes = xNode.SelectNodes("Options/Option");
                if (opNodes != null && opNodes.Count > 0)
                {
                    component.Options = new Dictionary<string, string>();
                    foreach (XmlNode opNode in opNodes)
                    {
                        string name = opNode.Attributes.GetNamedItem("Name").Value;
                        string value = opNode.Attributes.GetNamedItem("Value").Value;
                        component.Options.Add(name, value);
                    }
                }

                return component;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }
    }
}
